const app = getApp();
Page({
  data: {
    showDialog: false,
    info_tele: "",
    info_wechat: "",
    requ_name: "小花",
    requ_age: "12",
    requ_sex: "女",
    requ_ethnic: "回",
    requ_school: "西宁市一小",
    requ_grade: "5年级",
    requ_text: "我学习认真我学习认真我学习认真我学习认我学习认真我学习认真我学习认真我学习认",
    requ_file: "../../pages/imgs/箭头.png"
    },
  onLoad(options){
    //this.loadData();
    console.log(options.id);
    this.setData({
      id:options.id
    });
    this.loadData(options.id)
  },
  toIndex() {
    wx.navigateBack({
      delta: 1
    })
  },

  getNum() {
    this.setData({ showDialog: true });
  },

  inputTele(e) {
    let value = e.detail.value.replace(/\D/g, '');
    value = value.substring(0, 11);
    this.setData({ info_tele: value });
  },

  inputWechat(e) {
    this.setData({ info_wechat: e.detail.value.trim() }); // 新增trim去空格
  },

  handleConfirm() {
    // 新增双非空校验
    if (!this.data.info_tele) {
      wx.showToast({ title: '手机号不能为空', icon: 'none' });
      return;
    }
    if (!this.data.info_wechat) {
      wx.showToast({ title: '微信号不能为空', icon: 'none' });
      return;
    }
    // 原有格式校验
    if (!this.validatePhone()) {
      wx.showToast({ title: '请输入11位手机号', icon: 'none' });
      return;
    }

    const that = this;
    var linkurl=app.globalData.apiUrl;
    wx.request({
      url: linkurl+'/user/UserInformationCreate/'+this.data.id+'/',
      method: "POST",
      header: {
        'Authorization': wx.getStorageSync('token'),
      },
      data: {
        info_tele: this.data.info_tele,
        info_wechat: this.data.info_wechat
      },
      success(res) {
        console.log("提交成功", res);
        wx.showToast({ title: "提交成功" });
      },
      fail(err) {
        console.error("提交失败", err);
        wx.showToast({ title: "提交失败", icon: "none" });
      }
    });
    this.closeDialog();
  },

  validatePhone() {
    const tele = this.data.info_tele;
    return tele.length === 11 && /^\d+$/.test(tele);
  },

  handleCancel() {
    this.closeDialog();
  },

  closeDialog() {
    this.setData({
      showDialog: false,
      info_tele: "",
      info_wechat: ""
    });
  },
  loadData(id){
    
   
    var linkurl=app.globalData.apiUrl;
    wx.request({
      url: linkurl+'/user/UserRequirementDetail/'+id+'/',
      method:'GET',
      header: {
        'Authorization': wx.getStorageSync('token'),
      },
      success:(res)=>{
        console.log("返回数据：",res);
        var con=res.data.data;
        this.setData({
          requ_name:con.requ_name,
          requ_age: con.requ_age,
          requ_sex: con.requ_sex,
          requ_ethnic: con.requ_ethnic,
          requ_school: con.requ_school,
          requ_grade: con.requ_grade,
          requ_text: con.requ_text,
          requ_file:con.requ_file
        })
        
        

      },
      fail:(res)=>{
        console.log("获取数据失败:",res.data);
        this.setData({loading:false});
      }

    });
    
},
})